﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DanhMuc_RenLuyenDoanVien : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            MemberShip_ASP.BindDrop(drldonvi, this.Page.User.Identity.Name);
        }
    }
    
    private void BindGrid(int nam)
    {
        _dbDataContext _db = new _dbDataContext();
        string madonvi = drldonvi.SelectedValue.ToString().Trim();//MemberShip_ASP.GetMaDonViFromUserName(this.Page.User.Identity.Name);
        var val = from p in _db.GetListDoanVienRenLuyen(nam,madonvi)
                  select new
                  {
                      MaDoanVien = p.MaDonVien.Trim(),
                      TenDoanVien = p.HoTen.Trim(),
                      NgayVaoDoan=string.Format("{0:dd/MM/yyyy}",p.NgayVaoDoan),
                      Phai = (p.Phai == true ? "Nam" : "Nữ"),
                      NgaySinh = string.Format("{0:dd/MM/yyyy}", p.NgaySinh),
                      CMND = p.CMND
                  };
        grid_renluyendoanvien.DataSource = val;
        grid_renluyendoanvien.DataBind();
        if (grid_renluyendoanvien.Rows.Count != 0)
        {
            lblStatusLabel.Text = "DANH SÁCH ĐOÀN VIÊN";
        }
        else
        {
            lblStatusLabel.Text = "ĐOÀN VIÊN ĐÃ ĐĂNG KÝ RÈN LUYỆN TRONG NĂM "+txtnamrenluen.Text;
        }
    }
    //chọn all checkbox
    protected void chkInGridviewHeader_CheckedChanged(object sender, EventArgs e)//day la ham de goi trong header cua itemtemplate
    {
        CheckBox chk = (CheckBox)sender;
        CheckBox chkitem;
        foreach (GridViewRow dr in grid_renluyendoanvien.Rows)
        {
            chkitem = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                chkitem.Checked = true;
            }
            else
            {
                chkitem.Checked = false;
            }
        }
    }
    protected void btntruyvan_Click(object sender, EventArgs e)
    {
        BindGrid(int.Parse(txtnamrenluen.Text));
    }
    protected void btnsave_Click(object sender, EventArgs e)
    {
        string madoanvien = "";
        CheckBox chk;
        int nam = int.Parse(txtnamrenluen.Text);
        _dbDataContext _db = new _dbDataContext();
        tblRenLuyenDoanVien _tblrenluyen;
        foreach (GridViewRow dr in grid_renluyendoanvien.Rows)
        {
            chk = (CheckBox)dr.FindControl("chkchon");
            if (chk.Checked == true)
            {
                madoanvien = ((Label)dr.FindControl("lblMaDoanVien")).Text;
                _tblrenluyen = new tblRenLuyenDoanVien();
                _tblrenluyen.NamRenLuyen = nam;
                _tblrenluyen.MaDoanVien = madoanvien;
                _tblrenluyen.NgayDangKy = DateTime.Now;
                _tblrenluyen.MucDoHoanThanh = 1;
                _db.tblRenLuyenDoanViens.InsertOnSubmit(_tblrenluyen);
            }
        }
        _db.SubmitChanges();
        BindGrid(int.Parse(txtnamrenluen.Text));
    }
    protected void txtnamrenluen_TextChanged(object sender, EventArgs e)
    {
        //BindGrid(int.Parse(txtnamrenluen.Text));
    }
}